﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class PhysicalCountDiffRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15383;
        protected GridView gvPhysicalCountDiffRpt;
        protected GridView gvPhysicalCountDiffRptShowLot;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnShowLot;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblPhysicalCountDiffRptList;
        protected Label lblTotalAdjQty;
        protected Label lblTotalAdjQtyText;
        protected Label lblTotalReCountQty;
        protected Label lblTotalReCountQtyText;
        protected Label lblTotalSysQty;
        protected Label lblTotalSysQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalAdjQty;
        private decimal TotalReCountQty;
        private decimal TotalSysQty;
        protected UpdatePanel up1;

        public void BindData2gvPhysicalCountDiffRpt(PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(physicalCountDiffRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetPhysicalCountReportDataSet(physicalCountDiffRptQueryEntity);
            if (physicalCountDiffRptQueryEntity.ShowLot == "Y")
            {
                base.BindData2GridView(this.gvPhysicalCountDiffRptShowLot, ds);
            }
            else
            {
                base.BindData2GridView(this.gvPhysicalCountDiffRpt, ds);
            }
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
            physicalCountDiffRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = physicalCountDiffRptQueryEntity.CurrentPage;
            physicalCountDiffRptQueryEntity.CurrentPage = "0";
            if (physicalCountDiffRptQueryEntity.ShowLot == "Y")
            {
                this.gvPhysicalCountDiffRptShowLot.Columns[1].Visible = false;
                this.gvPhysicalCountDiffRptShowLot.AllowSorting = false;
                this.gvPhysicalCountDiffRptShowLot.ShowFooter = false;
            }
            else
            {
                this.gvPhysicalCountDiffRpt.Columns[1].Visible = false;
                this.gvPhysicalCountDiffRpt.AllowSorting = false;
                this.gvPhysicalCountDiffRpt.ShowFooter = false;
            }
            this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
            physicalCountDiffRptQueryEntity.IsGetAll = false;
            physicalCountDiffRptQueryEntity.CurrentPage = currentPageIndex;
            this.SetPhysicalCountDiffRptQueryEntity(physicalCountDiffRptQueryEntity);
            if (physicalCountDiffRptQueryEntity.ShowLot == "Y")
            {
                base.Export2Excel("gvPhysicalCountDiffRpt.xls", this.gvPhysicalCountDiffRptShowLot);
            }
            else
            {
                base.Export2Excel("gvPhysicalCountDiffRpt.xls", this.gvPhysicalCountDiffRpt);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
            physicalCountDiffRptQueryEntity.CurrentPage = "0";
            this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
            physicalCountDiffRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
            physicalCountDiffRptQueryEntity.OwnerID = "";
            physicalCountDiffRptQueryEntity.SkuID = "";
            physicalCountDiffRptQueryEntity.SkuClassID = "";
            physicalCountDiffRptQueryEntity.StartLoc = "";
            physicalCountDiffRptQueryEntity.EndLoc = "";
            this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
            if (int.Parse(physicalCountDiffRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                physicalCountDiffRptQueryEntity.CurrentPage = (int.Parse(physicalCountDiffRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
            if (int.Parse(physicalCountDiffRptQueryEntity.CurrentPage) > 0)
            {
                physicalCountDiffRptQueryEntity.CurrentPage = (int.Parse(physicalCountDiffRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
            this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
        }

        private PhysicalCountDiffRptQueryEntity GetPhysicalCountDiffRptQueryEntity()
        {
            return (base.PageQueryEntitySession as PhysicalCountDiffRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
                physicalCountDiffRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
            }
        }

        protected void gvPhysicalCountDiffRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15383", this.gvPhysicalCountDiffRpt);
        }

        protected void gvPhysicalCountDiffRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblSysQty")) != null)
                {
                    this.TotalSysQty += decimal.Parse((((Label) e.Row.FindControl("lblSysQty")).Text != "") ? ((Label) e.Row.FindControl("lblSysQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblReCountQty")) != null)
                {
                    this.TotalReCountQty += decimal.Parse((((Label) e.Row.FindControl("lblReCountQty")).Text != "") ? ((Label) e.Row.FindControl("lblReCountQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblAdjQty")) != null)
                {
                    this.TotalAdjQty += decimal.Parse((((Label) e.Row.FindControl("lblAdjQty")).Text != "") ? ((Label) e.Row.FindControl("lblAdjQty")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalSysQtyText.Text = base.GetPreferenceDigit(this.TotalSysQty);
                this.lblTotalReCountQtyText.Text = base.GetPreferenceDigit(this.TotalReCountQty);
                this.lblTotalAdjQtyText.Text = base.GetPreferenceDigit(this.TotalAdjQty);
            }
        }

        protected void gvPhysicalCountDiffRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = this.GetPhysicalCountDiffRptQueryEntity();
                physicalCountDiffRptQueryEntity.SortField = e.SortExpression;
                if (physicalCountDiffRptQueryEntity.SortDirection == "ASC")
                {
                    physicalCountDiffRptQueryEntity.SortDirection = "DESC";
                }
                else if (physicalCountDiffRptQueryEntity.SortDirection == "DESC")
                {
                    physicalCountDiffRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvPhysicalCountDiffRptShowLot_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15383", this.gvPhysicalCountDiffRptShowLot);
        }

        private void LoadData()
        {
            PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity = new PhysicalCountDiffRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    physicalCountDiffRptQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    physicalCountDiffRptQueryEntity.SkuClassID = this.PreviousPage.SkuClassID;
                    physicalCountDiffRptQueryEntity.SkuID = this.PreviousPage.SkuID;
                    physicalCountDiffRptQueryEntity.StartLoc = this.PreviousPage.StartLoc;
                    physicalCountDiffRptQueryEntity.EndLoc = this.PreviousPage.EndLoc;
                    physicalCountDiffRptQueryEntity.ShowLot = this.PreviousPage.ShowLot;
                    if (physicalCountDiffRptQueryEntity.ShowLot == "Y")
                    {
                        this.gvPhysicalCountDiffRptShowLot.Visible = true;
                        this.gvPhysicalCountDiffRpt.Visible = false;
                    }
                }
                else
                {
                    physicalCountDiffRptQueryEntity.OwnerID = "";
                    physicalCountDiffRptQueryEntity.SkuClassID = "";
                    physicalCountDiffRptQueryEntity.SkuID = "";
                    physicalCountDiffRptQueryEntity.StartLoc = "";
                    physicalCountDiffRptQueryEntity.EndLoc = "";
                    physicalCountDiffRptQueryEntity.ShowLot = "N";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            physicalCountDiffRptQueryEntity.IsGetAll = false;
            physicalCountDiffRptQueryEntity.CurrentPage = "0";
            if (physicalCountDiffRptQueryEntity.ShowLot == "Y")
            {
                physicalCountDiffRptQueryEntity.PageSize = this.gvPhysicalCountDiffRptShowLot.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvPhysicalCountDiffRptShowLot.PageSize;
            }
            else
            {
                physicalCountDiffRptQueryEntity.PageSize = this.gvPhysicalCountDiffRpt.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvPhysicalCountDiffRpt.PageSize;
            }
            physicalCountDiffRptQueryEntity.SortField = "PT.LOC,PT.SKU_ID";
            physicalCountDiffRptQueryEntity.SortDirection = "DESC";
            this.SetPhysicalCountDiffRptQueryEntity(physicalCountDiffRptQueryEntity);
            this.BindData2gvPhysicalCountDiffRpt(physicalCountDiffRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvPhysicalCountDiffRpt);
                base.InitAnalyticsReportGridview(this.gvPhysicalCountDiffRptShowLot);
                this.LoadData();
            }
        }

        public void SetPhysicalCountDiffRptQueryEntity(PhysicalCountDiffRptQueryEntity physicalCountDiffRptQueryEntity)
        {
            base.PageQueryEntitySession = physicalCountDiffRptQueryEntity;
        }

        public PhysicalCountDiffRptQuery PreviousPage
        {
            get
            {
                return (PhysicalCountDiffRptQuery) base.PreviousPage;
            }
        }
    }
}

